package Jsoup;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Created by 王念 on 2016/4/9.
 */
public class JsoupService {
    private static CloseableHttpClient httpClient = HttpClients.createDefault();

    public synchronized static  void test(int i) throws IOException, CloneNotSupportedException {
            System.out.println("第"+i+"行");
            HttpGet httpget = new HttpGet("http://www.baidu.com/p/sys/data/tieba/feed?rec=1000004&portrait=625d77616e676a6f6a6f61626364b53d&pn="+i+"&type=all&t=1460216825697");
            CloseableHttpResponse response = httpClient.execute(httpget);
            HttpEntity httpEntity = response.getEntity();
            String strResult = EntityUtils.toString(httpEntity);
            Document document = Jsoup.parse(strResult);
            Elements links1 = document.getElementsByClass("feed-content");
            for (Element link : links1) {
                String linkHref = link.attr("href");
                String url = linkHref.replace("\\x22", "").replace("\\", "");
                getContext(url, "wangjojoabcd");
            }
            httpget.clone();
            response.close();
    }

    public static void getContext(String url, String nicknameStr) throws IOException {
        HttpGet httpget1 = new HttpGet(url);
        CloseableHttpResponse response1 = httpClient.execute(httpget1);
        HttpEntity httpEntity1 = response1.getEntity();
        String strResult1 = EntityUtils.toString(httpEntity1);
        Document document1 = Jsoup.parse(strResult1);
        //找到大DIV
        Elements links2 = document1.getElementsByClass("l_post_bright");
        for (Element link : links2) {
            //找到发帖的人
            Elements links3 = link.getElementsByClass("p_author_name");
            Element link1 = links3.get(0);
            String nickname = link1.text();
            if (nicknameStr == null) {
                method1("d:/input1.text", link.text()+",url:"+url);
            } else if (nicknameStr.equals(nickname)) {
                method1("d:/input1.text", link.text()+",url:"+url);
            }
        }
    }
    public   static   void  method1(String file, String conent) {
        BufferedWriter out = null ;
        try  {
            out = new  BufferedWriter( new OutputStreamWriter( new FileOutputStream(file,  true )));
            out.newLine();
            out.write(conent);
        } catch  (Exception e) {
            e.printStackTrace();
        } finally  {
            try  {
                out.close();
            } catch  (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
